package ks.server.controllers.commands.admin.plugin;

import ks.framework.common.Message;
import ks.framework.communicator.Communicator;
import ks.framework.interfaces.IProcessMessage;
import ks.managers.plugin.Plugin;
import ks.managers.plugin.PluginManager;
import ks.server.ServerContext;
import ks.server.ipc.Listener;
import ks.server.persist.PersistentPluginManager;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

import java.net.MalformedURLException;


/**
 * Server-side controller for processing an admin-command to clear a plugin's stats (wins/losses)
 * @author harry moreno
 *
 */
public class PluginClearController implements IProcessMessage {

	/** Primary agent to whom we can communicate our messages. */
	Communicator com;
	
	/** regular expression indicating how to divide xml */
	String delims = "[/.]+";	//divides string by '/' and '.'
	
	/** Attributes from XML string. */
	public static final String pluginAtt = "plugin";
	
	public PluginClearController (Communicator com) {
		this.com = com;
	}
	/** 
	 * plugin-clear Message comes in from Client.
	 * 
	 *  <plugin-clear plugin="Heineman_Klondike"/>
	 * @throws MalformedURLException 
	 */
	public boolean process(Message m){
		PersistentPluginManager pm = ServerContext.getPluginManager();
		Node pluginClear = m.contents();
		
		if (pluginClear == null) {
			Message r = Listener.nack(m, 4, "Unable to clear Plugin: pluginClear null.");  // low severity level
			r.setRecipient(m.getOriginator());
	        r.setBroadcast();
	        com.distribute(r);
			return false;
		}
		
		// Find out plugin's name
		NamedNodeMap nnp = pluginClear.getAttributes();
		
		Node pluginNode = nnp.getNamedItem("plugin");
		
		if (pluginNode == null) {
			Message r = Listener.nack(m, 4, "Unable to clear Plugin: pluginNode null.");  // low severity level
			r.setRecipient(m.getOriginator());
	        r.setBroadcast();
	        com.distribute(r);
			return false;
		}
		
		String pluginName = pluginNode.getNodeValue();

		// Ask PluginManager to clear the plugin.
		try {
			Plugin pluginToClear = pm.getPlugin(pluginName);
			
			if (pluginToClear != null) {
				// successful operation!
				pluginToClear.setWins(0);
				pluginToClear.setLosses(0);
				pm.updatePlugin(pluginName);
			} else {
				Message r = Listener.nack(m, 4, "Unable to clear Plugin: pluginToClear null.");  // low severity level
				r.setRecipient(m.getOriginator());
		        r.setBroadcast();
		        com.distribute(r);
				return false;
			}
		/*
			// Broadcast plugins to everyone. Ask PluginManager for collection of plugins, 
			// and form this request from this set.
	        StringBuilder sb = new StringBuilder("<request version='1.0' id='1'><plugins>");
	        for (Plugin plugin : pm.getList()) {
	        	sb.append ("<plugin name=\"" + plugin.name + "\" uri=\"" + plugin.location + "\"/>\n");
	        }
	        sb.append("</plugins></request>");
	        Document d = Message.construct(sb.toString());
	        
	        // send to everyone INCLUDING the originator of the Message m.
	        Message r = new Message (d);*/
			
			Message r = Listener.ack(m, "Success");
	        r.setRecipient(m.getOriginator());
	        r.setBroadcast();
	        
	        com.distribute(r);
	        return true;
		} catch (Exception e) {
			e.printStackTrace();

			return false;
		}
	}
}
